from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone


def default_datetime():    
    now = timezone.now()   
    return now


# 订单详情分析表
class OrderDetails(models.Model):
    order_date = models.DateField()
    cus_name = models.CharField(max_length=100)
    cus_type = models.CharField(max_length=200)
    sales_vol = models.FloatField(blank=True, null=True)
    sales_num = models.IntegerField()
    discount = models.FloatField(blank=True, null=True)
    profit = models.FloatField()
    order_category = models.CharField(max_length=200, blank=True, null=True)
    cate_items = models.CharField(max_length=200)
    order_name = models.CharField(max_length=255)
    iss_date = models.DateField()
    area = models.CharField(max_length=255, blank=True, null=True)
    city = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'order_details'


# 经销商绩效分析表
class DealerAnalysis(models.Model):
    area = models.CharField(max_length=200)
    area_manager = models.CharField(max_length=255)
    sales_vol = models.IntegerField()
    profit = models.IntegerField()
    profit_margin = models.FloatField()
    cus_num = models.IntegerField()
    unit_price = models.IntegerField()
    per_order = models.FloatField()
    sales_num = models.IntegerField()

    class Meta:
        db_table = 'dealer_analysis'


# 客户消费行为分析表
class CustomerAnalysis(models.Model):
    cus_name = models.CharField(max_length=100)
    cus_type = models.CharField(max_length=200)
    consumption = models.IntegerField()
    profit = models.IntegerField()
    profit_margin = models.FloatField()
    order_num = models.IntegerField()
    unit_price = models.IntegerField()
    city_num = models.IntegerField()
    firstyear = models.TextField()  # This field type is a guess.

    class Meta:
        db_table = 'customer_analysis'

# 品类销售分析表
class CategoryAnalysis(models.Model):
    cate_items = models.CharField(max_length=200)
    order_category = models.CharField(max_length=200)
    sales_vol = models.IntegerField()
    profit = models.IntegerField()
    profit_margin = models.FloatField()
    sales_num = models.IntegerField()
    cus_num = models.IntegerField()
    per_order = models.FloatField()
    avg_price = models.IntegerField()

    class Meta:
        db_table = 'category_analysis'


# 物流配送分析表
class LogisticsAnalysis(models.Model):
    order_date = models.DateField()
    order_category = models.CharField(max_length=200)
    cate_items = models.CharField(max_length=200)
    order_name = models.CharField(max_length=255)
    delivery_days = models.IntegerField()
    area = models.CharField(max_length=255)

    class Meta:
        db_table = 'logistics_analysis'


# 销售数据概览表
class SalesData(models.Model):
    order_date = models.DateField()
    cus_name = models.CharField(max_length=100)
    cus_type = models.CharField(max_length=200)
    sales_vol = models.FloatField(blank=True, null=True)
    sales_num = models.IntegerField()
    discount = models.FloatField(blank=True, null=True)
    profit = models.FloatField()
    order_category = models.CharField(max_length=200, blank=True, null=True)
    cate_items = models.CharField(max_length=200)
    order_name = models.CharField(max_length=255)
    iss_date = models.DateField()
    area = models.CharField(max_length=255, blank=True, null=True)
    city = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        db_table = 'sales_data'


class MyUploadFileRecord(models.Model):
    file_path = models.CharField(verbose_name="文件路径", max_length=200)
    file_name = models.CharField(verbose_name="文件名称", max_length=100)
    user = models.ForeignKey(User, related_name='file', on_delete=models.CASCADE)
    file_type = models.IntegerField(verbose_name="excel类型", choices=((0,"销售数据概览表"),
                                                                       (1, "品类销售分析表"),
                                                                       (2, "客户消费行为分析表"),
                                                                       (3, "物流配送分析表"),
                                                                       (4, "经销商绩效分析表"),
                                                                       (5, "订单详情分析表")))
    upload_time = models.DateTimeField(auto_now_add=True, verbose_name="上传时间")


class MyImageRecord(models.Model):
    image_path = models.CharField(verbose_name="文件路径", max_length=200)
    image_name = models.CharField(verbose_name="文件名称", max_length=100)
    user = models.ForeignKey(User, related_name='image', on_delete=models.CASCADE)
    upload_time = models.DateTimeField(auto_now_add=True, verbose_name="上传时间")




class BoardOrder(models.Model):
  creator=models.ForeignKey(User, on_delete=models.CASCADE)
  order=models.TextField()
  id=models.CharField(max_length=64, primary_key=True)
  created_at = models.DateTimeField(db_index=True, default=default_datetime)
  updated_at = models.DateTimeField(db_index=True, null=False)



# Create your models here.
class Chart(models.Model):
    chart_id=models.CharField(max_length=64, primary_key=True)
    chart_name=models.CharField(max_length=128)
    desc=models.CharField(max_length=512, null=True)
    content=models.TextField()
    is_private=models.BooleanField()
    status=models.IntegerField()
    creator=models.ForeignKey(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(db_index=True, default=default_datetime)
    updated_at = models.DateTimeField(db_index=True, null=False)

class Dashboard(models.Model):
  dashboard_id=models.CharField(max_length=64, primary_key=True)
  name=models.CharField(max_length=128)
  desc=models.CharField(max_length=512, null=True)
  content=models.TextField()
  is_private=models.BooleanField()
  status=models.IntegerField()
  creator=models.ForeignKey(User, on_delete=models.CASCADE)
  created_at = models.DateTimeField(db_index=True, default=default_datetime)
  updated_at = models.DateTimeField(db_index=True, null=False)

class ChartBoardMap(models.Model):
  chart=models.ForeignKey(Chart, on_delete=models.CASCADE)
  dashboard=models.ForeignKey(Dashboard, on_delete=models.CASCADE)
  id=models.CharField(max_length=64, primary_key=True)
  created_at = models.DateTimeField(db_index=True, default=default_datetime)
  updated_at = models.DateTimeField(db_index=True, null=False)

